package com.controller;

import com.pojo.ConsumerProductRelationshipRel;
import com.pojo.view.ShopCartView;
import com.service.iter.IBillService;
import com.service.iter.IProductsService;
import com.util.Res;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 购物车控制器
 *
 * @author 蔡文欣
 * @date 2023/07/03 00:43:21
 */
@RestController
@RequestMapping("shopCart")
public class ShopCartController {
    /**
     * 购物车服务
     */
    @Autowired
    private IProductsService shopCartService;
    @Autowired
    private IBillService billService;

    /**
     * 购物车
     *
     * @param phone 电话
     * @return {@link Res}
     */
    @GetMapping("/{phone}")
    public Res shopCart(@PathVariable String phone) {
        Res res = new Res();
        List<ShopCartView> shopCartViews = shopCartService.byConsumerPhoneGetShops(phone);
        if (Objects.isNull(phone)) {
            res.setMsg("请先登录再查看购物车");
            res.setCode(500);
        }
        if (shopCartViews.size() < 1) {
            res.setMsg("尚未选择商品");
            res.setCode(404);
        } else {
            res.setData(shopCartViews);
            res.setCode(200);
        }
        return res;
    }

    /**
     * 更新购物车
     *
     * @param data 数据
     * @return {@link Res}
     */
    @PutMapping("/updateShopCart")
    public Res updateShopCart(@RequestBody ConsumerProductRelationshipRel data) {

        System.out.println(data);
        return shopCartService.updateShopCart(data);
    }
    /**
     * 更新大小
     *
     * @param data 数据
     * @return {@link Res}
     */
    @PutMapping("/updateSize")
    public Res updateSize(@RequestBody ConsumerProductRelationshipRel data) {

        return shopCartService.updateSize(data);
    }

    /**
     * 添加到购物车
     *
     * @param ShopProduct 商店产品
     * @return {@link Res}
     */
    @PostMapping("/addToShopCart")
    public Res addToShopCart(@RequestBody ConsumerProductRelationshipRel ShopProduct) {
        return shopCartService.addShopCart(ShopProduct);
    }

    /**
     * 移除购物车
     *
     * @param id id
     * @return {@link Res}
     */
    @DeleteMapping("/{id}")
    public Res removeShopCart(@PathVariable Long id) {
        return shopCartService.removeShopCart(id);
    }

    /**
     * 清空购物车
     *
     * @param phone 电话
     * @return {@link Res}
     */
    @DeleteMapping("emptyShopCart/{phone}")
    public Res emptyShopCart(@PathVariable String phone) {
        return shopCartService.emptyShopCart(phone);
    }

    @GetMapping("bill/{phone}")
    public Res bill(@PathVariable String phone) {
        return billService.byPhoneGetPrice(phone);
    }
}
